package com.ruyuan.eshop.seller.controller.seller.aftersale;

import com.ruyuan.eshop.framework.context.user.UserContext;
import com.ruyuan.eshop.framework.core.WebPage;
import com.ruyuan.eshop.common.domain.dto.AfterSaleQueryParam;
import com.ruyuan.eshop.common.domain.vo.ApplyAfterSaleVO;
import com.ruyuan.eshop.common.domain.vo.PutInWarehouseVO;
import com.ruyuan.eshop.common.enums.Permission;
import com.ruyuan.eshop.common.service.aftersale.AfterSaleService;
import com.ruyuan.eshop.common.service.aftersale.AfterSaleQueryService;
import com.ruyuan.eshop.common.service.aftersale.SellerCreateTradeService;
import com.ruyuan.eshop.seller.domain.request.AfterSaleCloseRequest;
import com.ruyuan.eshop.seller.domain.request.AfterSaleServiceAuditRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 售后服务相关API
 * @author pwd
 * @version 1.0
 */
@Api(description = "售后服务相关API")
@RestController
@RequestMapping("/api/v1.0/after-sales")
@Validated
public class AfterSaleSellerController {

    @Autowired
    private AfterSaleService afterSaleService;

    @Autowired
    private AfterSaleQueryService afterSaleQueryService;

    @Autowired
    private SellerCreateTradeService sellerCreateTradeService;

    /**
     * 获取申请售后服务记录列表
     * @param param
     * @return
     */
    @ApiOperation(value = "获取申请售后服务记录列表")
    @GetMapping
    public WebPage list(@Valid AfterSaleQueryParam param){
        param.setSellerId(UserContext.getSeller().getSellerId());
        return afterSaleQueryService.list(param);
    }

    /**
     * 获取售后服务详细信息
     * @param serviceSn
     * @return
     */
    @ApiOperation(value = "获取售后服务详细信息")
    @GetMapping(value = "/detail/{service_sn}")
    public ApplyAfterSaleVO detail(@PathVariable("service_sn") String serviceSn){
        return afterSaleQueryService.detail(serviceSn, Permission.SELLER);
    }

    /**
     * 商家审核售后服务申请
     * @param serviceSn
     * @param afterSaleServiceAuditRequest
     */
    @ApiOperation(value = "商家审核售后服务申请")
    @PostMapping(value = "/audit/{service_sn}")
    public void audit(@PathVariable("service_sn") String serviceSn,
                      @RequestBody AfterSaleServiceAuditRequest afterSaleServiceAuditRequest) {
        this.afterSaleService.audit(serviceSn,
                afterSaleServiceAuditRequest.getAuditStatus(),
                afterSaleServiceAuditRequest.getRefundPrice(),
                afterSaleServiceAuditRequest.getReturnAddr(),
                afterSaleServiceAuditRequest.getAuditRemark());
    }

    /**
     * 商家为售后服务手动创建新订单
     * @param serviceSn
     */
    @ApiOperation(value = "商家为售后服务手动创建新订单")
    @PostMapping(value = "/create-order/{service_sn}")
    public void createOrder(@PathVariable("service_sn") String serviceSn) {
        this.sellerCreateTradeService.sellerCreateTrade(serviceSn);
    }

    /**
     * 商家关闭售后服务单
     * @param serviceSn
     * @param afterSaleCloseRequest
     */
    @ApiOperation(value = "商家关闭售后服务单")
    @PostMapping(value = "/close/{service_sn}")
    public void close(@PathVariable("service_sn") String serviceSn,
                      @RequestBody AfterSaleCloseRequest afterSaleCloseRequest) {
        this.afterSaleService.closeAfterSale(serviceSn, afterSaleCloseRequest.getReason());
    }

    /**
     * 商家将申请售后服务退还的商品入库
     * @param putInWarehouseVO
     */
    @ApiOperation(value = "商家将申请售后服务退还的商品入库")
    @PostMapping(value = "/put-in/warehouse")
    public void putInWarehouse(@Valid @RequestBody PutInWarehouseVO putInWarehouseVO) {
        this.afterSaleService.putInWarehouse(putInWarehouseVO.getServiceSn(), putInWarehouseVO.getStorageList(),
                putInWarehouseVO.getRemark());
    }

}
